package campus.aiit.ssl.util;

import com.iflytek.uaac.client.rest.context.UaacServiceContext;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpSession;

public class ContextUtil {
  public static HttpSession httpSession(){
    ServletRequestAttributes attributes;
    // whether is a request
    try {
      attributes = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
    } catch (IllegalStateException e) {
      // e.printStackTrace();
      System.out.println("Non-Request(Application Internal Task) Execute the query SQL");
      return null;
    }
    return attributes.getRequest().getSession(false);
  }

  public static String userId() {
    HttpSession httpSession = httpSession();
    return httpSession != null ? UaacServiceContext.getUaacService().getCurrentLoginName(httpSession) : null;
  }
}